package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.util.JsonResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("region")
public class RegionController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    /**
     * 查询区域地区
     *
     * @param qo    查询对象
     * @param model 参数共享对象
     * @return 返回页面
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") RegionQuery qo, Model model) {
        PageInfo<Region> pageInfo = regionService.listAllByPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "region/list";
    }


    /**
     * 通过deep查询目的地
     *
     * @param deep 地域级别
     * @return List<Region></>
     */
    @RequestMapping("getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) {
        return destinationService.getDestByDeep(deep);
    }

    /**
     * 保存和修改地区
     *
     * @param region 地区
     * @return 返回结果是不是成功
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        regionService.saveOrUpdate(region);
        return JsonResult.SUCCESS;
    }

    /**
     * 通过id查询关联地区
     * @param rid 区域id
     * @return 关联地区集合
     */
    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(Long rid) {
        Region region = regionService.getRegionById(rid);
        Long[] refIds = region.getRefIds();
        return   destinationService.queryRefByIds(refIds);
    }

    /**
     * 通过id删除数据
     * @param id id
     * @return 删除是否成功
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object deleteById(Long id) {
        regionService.deleteById(id);
        return JsonResult.SUCCESS;
    }

    //hot=0&id=2
    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue(Long id, boolean hot) {
        regionService.changeHotValue(id, hot);
        return JsonResult.SUCCESS;
    }

}
